﻿
using AutoMapper;
using D.Traceability.Domain;
using D.Traceability.ErrorCode;
using D.Traceability.Infrastructure.Infrastructure;
using D.Traceability.Write.API.APPlicant.Command;
using D.Traceability.Write.API.APPlicant.Command.TestCommand;
using MediatR;

namespace D.Traceability.Write.API.APPlicant.CommandHandler.TestCommandHandler
{
    /// <summary>
    ///  添加质检报道图片
    /// </summary>
    public class AddTestCommandHandler : IRequestHandler<AddTestCommand, APIResult<string>>
    {
        private readonly IBaseRepository<SU_PictureModel> baseRepository;
        private readonly IMapper mapper;

        public AddTestCommandHandler(IBaseRepository<SU_PictureModel> baseRepository, IMapper mapper)
        {
            this.baseRepository = baseRepository;
            this.mapper = mapper;
        }

        /// <summary>
        /// 添加质检报道图片
        /// </summary>
        public async Task<APIResult<string>> Handle(AddTestCommand request, CancellationToken cancellationToken)
        {
            APIResult<string> result = new APIResult<string>();

            var model = mapper.Map<SU_PictureModel>(request);
            int res = await baseRepository.Add(model);
            if (res > 0)
            {
                result.Code = APIEnum.添加成功;
                result.Msg = APIEnum.添加成功.ToString();
                result.data = res.ToString();
            }
            else
            {
                result.Code = APIEnum.添加失败;
                result.Msg = APIEnum.添加失败.ToString();
                result.data = res.ToString();
            }
            return await Task.FromResult(result);


        }
    }
}
